package com.zw.controller;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.websocket.server.PathParam;

import org.apache.ibatis.annotations.Param;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.zw.entity.Commodity;
import com.zw.entity.CommodityType;
import com.zw.javaClass.Test2;
import com.zw.service.CommodityService;
import com.zw.service.CommodityTypeService;

@Controller
@RequestMapping("/CommodityController")
public class CommodityController {

	@Autowired
	private CommodityService commodityService;
	@Autowired
	private CommodityTypeService commodityTypeService;

	@Autowired
	private Test2 test2;

	@RequestMapping("/show")
	public ModelAndView showCommodity(){
		List<CommodityType> typeList=commodityTypeService.selectCommodityTypeAll();
		List<Commodity> list=commodityService.selectCommodity(typeList.get(0).getCommoditytypeid());
		ModelAndView mav=new ModelAndView("/index");
		mav.addObject("commodityList",list);
		mav.addObject("commodityListTypeList",typeList);
		return mav;
	}

	@RequestMapping("/importCommodity")
	public String importCommodity(@RequestParam("filePath") MultipartFile file){
		try {
			FileInputStream fileInputStream=(FileInputStream) file.getInputStream();
			List<Commodity> list=test2.loadScoreInfo(fileInputStream);
			commodityService.insertCommodity(list);
		} catch (IOException e) {
			e.printStackTrace();
			return "redirect:/404";
		}
		return "CommodityController/redirect:/show";
	}

	@RequestMapping("/queryCommodity")
	@ResponseBody
	public List<Commodity> queryCommodity(int id){
		List<Commodity> list=commodityService.selectCommodity(id);
		return list;
	}

	@RequestMapping("/selectCommodity")
	public ModelAndView selectCommodity(@PathParam("id") int id){
		Commodity commodity=commodityService.select(id);
		ModelAndView modelAndView=new ModelAndView("/purchase");
		modelAndView.addObject("commodity",commodity);
		return modelAndView;
	}
	
	private void createTitle(XSSFWorkbook workbook, XSSFSheet sheet)  
	{  
		XSSFRow row = sheet.createRow(0);  
		//设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度  
		sheet.setColumnWidth(2, 12*256);  
		sheet.setColumnWidth(3, 17*256);  

		//设置为居中加粗  
		XSSFCellStyle style = workbook.createCellStyle();  
		XSSFFont font = workbook.createFont();  
		font.setBold(true);  
		style.setAlignment(XSSFCellStyle.ALIGN_CENTER);  
		style.setFont(font);  

		XSSFCell cell;  
		cell = row.createCell(0);  
		cell.setCellValue("ID");  
		cell.setCellStyle(style);  

		cell = row.createCell(1);  
		cell.setCellValue("Name");  
		cell.setCellStyle(style);  

		cell = row.createCell(2);  
		cell.setCellValue("Count");  
		cell.setCellStyle(style);  

		cell = row.createCell(3);  
		cell.setCellValue("TypeId");  
		cell.setCellStyle(style); 

		cell = row.createCell(4);  
		cell.setCellValue("Price");  
		cell.setCellStyle(style);
	}  

	/*** 
	 * 获取excel数据 
	 * @return 返回文件名称及excel文件的URL 
	 * @throws IOException 
	 */  
	@RequestMapping("/getExcel")  
	public ResponseEntity<?> getExcel() throws IOException  
	{  
		XSSFWorkbook workbook = new XSSFWorkbook();  
		XSSFSheet sheet = workbook.createSheet("商品表");  
		createTitle(workbook, sheet);  
		List<Commodity> list=commodityService.selectAll();

		int rowNum = 1;  
		for (Commodity c:list) {  

			XSSFRow row = sheet.createRow(rowNum);  
			row.createCell(0).setCellValue(c.getCommodityid());  
			row.createCell(1).setCellValue(c.getCommodityname());  
			row.createCell(2).setCellValue(c.getCommoditycount());  
			row.createCell(3).setCellValue(c.getCommoditytypeid());  
			row.createCell(4).setCellValue(c.getCommodityprice().doubleValue());  
			row.createCell(5);  
			rowNum++;  
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();  
		workbook.write(out);  
		HttpHeaders headers = new HttpHeaders();

		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		String exportName = "comm.xlsx";
		headers.setContentDispositionFormData("attachment",
				new String(exportName.getBytes("utf-8"),
						"ISO-8859-1"));
		return new ResponseEntity<>(out.toByteArray(),
				headers,
				HttpStatus.OK);
	}
}
